package com.meatball.game.service.impl

import com.meatball.game.data.Room
import com.meatball.game.service.RoomCacheService
import org.springframework.stereotype.Service
import java.util.concurrent.ConcurrentHashMap

@Service
class RoomCacheServiceImpl : RoomCacheService {
    private val cache = ConcurrentHashMap<String, Room>()
    override fun addRoom(room: Room) {
        cache[room.id] = room
    }

    override fun removeRoom(roomId: String) {
        cache.remove(roomId)
    }

    override fun getRoom(roomId: String) : Room? {
        return cache[roomId]
    }

    override fun getAllRoom(): Map<String, Room> {
        return cache
    }

    override fun getRoomByUserId(userId: String): Room? {
        return cache.values.find { room -> room.users.any { user -> user.id == userId } }
    }
}